/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.webapp.dao.UserDAO;
import vn.webapp.dto.DataPage;
import vn.webapp.model.Role;
import vn.webapp.model.User;
import vn.webapp.model.UserRoles;
import vn.webapp.model.Users;

@Service("userService")
public class UserServiceImpl implements UserService, UserDetailsService {

    @Autowired
    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    /**
     * Get an user by username
     * @param String
     * @return object
     * @throws UsernameNotFoundException
     */
    @Override
    public User loadUserByUsername(final String username) throws UsernameNotFoundException {
        Users users = null;
        try {
            users = userDAO.getByUsername(username);
            if (users != null) {
                User user = new User();
                user.setUsername(users.getUsername());
                user.setPassword(users.getPassword());
                user.setSalt(users.getSalt());
                user.setEmail(users.getEmail());
                
                return user;
            } else {
                return null;
            }
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            return null;
        }
    }
    
    /**
     * Get an user by username and id
     * @param String
     * @param int
     * @return object
     * @throws UsernameNotFoundException
     */
    public Users loadUserByUsernameAndId(final String username, int id) throws UsernameNotFoundException {
    	Users users = null;
        try {
            users = userDAO.getByUsernameAndId(username, id);
            return users;
        } catch (Exception e) {
            System.out.println("Exception: " + e.getMessage());
            return users;
        }
    }
    

    /**
     * Get list all users
     * @param null
     * @return List
     */
    @Override
    public DataPage<Users> list() {
        DataPage<Users> dataUser = new DataPage<>();
        dataUser.setData(userDAO.list());
        return dataUser;
    }

    /**
     * Get list all users
     * @param int
     * @return object
     */
    @Override
    public List<Users> viewDetail(int id) {
        return userDAO.viewDetail(id);
    }

    /**
     * Get list all users
     * @param int
     * @return int
     */
    @Override
    public int removeUser(String id) {
        return userDAO.removeUser(id);
    }

    /**
     * Get list all users
     * @param String
     * @param String
     * @param String
     * @param String
     * @return int
     */
    @Override
    public int saveAUser(String username, String password, String salt, String email, String role, int enabled)
    {
        Users user = new Users();
        user.setEmail(email);
        user.setUsername(username);
        user.setPassword(password);
        user.setSalt(salt);
        user.setEnabled(enabled);
        user.setUser_Code(username);
        
        UserRoles userRole = new UserRoles();
        userRole.setRole(role);
        userRole.setUsername(username);
        
        int i_SaveUser = userDAO.saveAUser(user);
        int i_SaveUserRole = userDAO.saveAUserRole(userRole);
        return i_SaveUser & i_SaveUserRole;
    }
    
    /**
     * Get list all users
     * @param String
     * @param String
     * @param String
     * @param String
     * @return int
     */
    @Override
    public void editAnUser(String username, String password, String email, String role, int activated){
    	Users user = new Users();
        user.setEmail(email);
        user.setUsername(username);
        user.setEnabled(activated);
        user.setUser_Code(username);
        user.setSalt(username);
        
        if(password != null){
        	user.setPassword(password);
        }
        
        userDAO.editAnUser(user);
    }
}
